package com.hsh.gaoqianlianmeng.domain;

import com.hsh.gaoqianlianmeng.anno.Alia;
import com.hsh.gaoqianlianmeng.constants.DateTimeConstants;
import com.hsh.gaoqianlianmeng.utils.DateUtils;
import lombok.Data;

import java.time.LocalDate;

/**
 * 处方签
 */
@Data
public class Prescription {

    @Alia("订单号")
    private String no;

    @Alia("身份证号")
    private String customerIdNo;

    @Alia("姓名")
    private String customerName;

    @Alia("性别")
    private String sex;

    @Alia("年龄")
    private Integer age;

    @Alia("出生年月日")
    private String birthdayStr;

    @Alia("数量")
    private Integer num;

    @Alia("开具日期")
    private String dateStr;


    @Alia("商品编码")
    private String productCode;

    @Alia("商品名称")
    private String productName;

    @Alia("科室")
    private String department;

    @Alia("诊断")
    private String diagnosis;

    @Alia("用法用量")
    private String usage;

    public Prescription init() {
        String sexCode = this.customerIdNo.substring(16, 17);
        this.sex = (Integer.valueOf(sexCode) % 2 == 1) ? "男" : "女";

        int birthYear = Integer.valueOf(this.customerIdNo.substring(6, 10));
        int birthMonth = Integer.valueOf(this.customerIdNo.substring(10, 12));
        int birthDay = Integer.valueOf(this.customerIdNo.substring(12, 14));

        this.birthdayStr = birthYear + "-" + birthMonth + "-" + birthDay;

        LocalDate now = LocalDate.now();
        int nowMonth = now.getDayOfMonth() - birthDay >= 0 ? now.getMonthValue() : now.getMonthValue() - 1;
        int nowYear = nowMonth - birthMonth >= 0 ? now.getYear() : now.getYear() - 1;
        this.age = nowYear - birthYear;

        this.dateStr = DateUtils.dateToStr(now, DateTimeConstants.NORM_DATE_PATH_PATTERN);

        return this;
    }


    public Prescription init(Product product) {
        this.init();
        this.department = product.getDepartment();
        this.diagnosis = product.getDiagnosis();
        this.usage = product.getUsage();
//        this.productName = product.getRegisteredName();
        return this;
    }
}
